#include "sr.h"

sr_type sr;
uint32_t u32srbuf;

static const int spiClk = 1000000; // 1 MHz
SPIClass * vspi = NULL;
SPIClass * hspi = NULL;

void sr_init(void)
{
    HV_CLK_L;HV_DIN_L;HV_LE_L;
    sr_senddata(0x0);
    sr.state = 0;
}

void sr_senddata(uint32_t u32sendbuf)
{
    uint32_t tmp = u32sendbuf;
    for(uint8_t i = 0; i < 32; i ++)
    {
        if(tmp & 1)
        {
            HV_DIN_H;
        }
        else
        {
            HV_DIN_L;
        }
        tmp >>= 1;
        
        HV_CLK_H;
        HV_CLK_L;
    }
    HV_LE_H;
    HV_LE_L;
    HV_DIN_L;
}

void sr_display(uint8_t num, bool N2_LD, bool N2_RD, bool N1_LD, bool N1_RD)
{
    u32srbuf = 0x0;
    uint8_t n1 = num % 10;
    uint8_t n2 = num / 10 % 10;
    switch(n1)
    {
        case 0:u32srbuf |= 1 << n1_0;break;
        case 1:u32srbuf |= 1 << n1_1;break;
        case 2:u32srbuf |= 1 << n1_2;break;
        case 3:u32srbuf |= 1 << n1_3;break;
        case 4:u32srbuf |= 1 << n1_4;break;
        case 5:u32srbuf |= 1 << n1_5;break;
        case 6:u32srbuf |= 1 << n1_6;break;
        case 7:u32srbuf |= 1 << n1_7;break;
        case 8:u32srbuf |= 1 << n1_8;break;
        case 9:u32srbuf |= 1 << n1_9;break;
        default:break;
    }
    switch(n2)
    {
        case 0:u32srbuf |= 1 << n2_0;break;
        case 1:u32srbuf |= 1 << n2_1;break;
        case 2:u32srbuf |= 1 << n2_2;break;
        case 3:u32srbuf |= 1 << n2_3;break;
        case 4:u32srbuf |= 1 << n2_4;break;
        case 5:u32srbuf |= 1 << n2_5;break;
        case 6:u32srbuf |= 1 << n2_6;break;
        case 7:u32srbuf |= 1 << n2_7;break;
        case 8:u32srbuf |= 1 << n2_8;break;
        case 9:u32srbuf |= 1 << n2_9;break;
        default:break;
    }
    if(N2_LD)
    {
        u32srbuf |= 1 << n2_l;
    }
    if(N2_RD)
    {
        u32srbuf |= 1 << n2_r;
    }
    if(N1_LD)
    {
        u32srbuf |= 1 << n1_l;
    }
    if(N1_RD)
    {
        u32srbuf |= 1 << n1_r;
    }
    
    sr_senddata(u32srbuf);
}


